

package cn.shengchao.examstar.teacher.ohs.local.appservice;

import cn.shengchao.examstar.me.course.ohs.local.appervices.MyCourseAppService;
import cn.shengchao.examstar.product.ohs.local.appservice.CourseService;
import cn.shengchao.examstar.product.ohs.local.message.CourseListDto;
import cn.shengchao.examstar.teacher.acl.ports.message.TeacherInfo;
import cn.shengchao.examstar.teacher.acl.ports.repositories.ITeacherRepository;
import cn.shengchao.examstar.teacher.ohs.local.message.TeacherDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherAppService {

    @Autowired
    private ITeacherRepository iTeacherRepository;

    @Autowired
    private CourseService courseService;

    @Autowired
    private MyCourseAppService myCourseAppService;

    public TeacherDetails getTeacherInfo(Long teacherId) {
        TeacherDetails teacherDetails = initTeacher(teacherId);
        setShowSum(teacherDetails);
        setCourseList(teacherDetails);
        setStudentSum(teacherDetails);
        return teacherDetails;
    }

    private void setCourseList(TeacherDetails teacherDetails) {
        List<CourseListDto> courseListDtoList = courseService.getCourseListByTeacherId(teacherDetails.getId());
        teacherDetails.setCourseList(courseListDtoList);
        teacherDetails.setCourseSum((long)courseListDtoList.size());
    }

    private void setStudentSum(TeacherDetails teacherDetails) {
        List<Long> courseIds = teacherDetails.getCourseList().stream().map(CourseListDto::getId).toList();
        teacherDetails.setStudentSum(myCourseAppService.getStudentSum(courseIds));
    }

    private void setShowSum(TeacherDetails teacherDetails) {
        Long sum = iTeacherRepository.getShowSum(teacherDetails.getId());
        teacherDetails.setShowSum(sum);
    }

    private TeacherDetails initTeacher(Long teacherId) {
        TeacherInfo teacherInfo = iTeacherRepository.findById(teacherId);
        TeacherDetails teacherDetails = new TeacherDetails();
        teacherDetails.setId(teacherInfo.getId());
        teacherDetails.setName(teacherInfo.getName());
        teacherDetails.setHeadUrl(teacherInfo.getHeadUrl());
        teacherDetails.setDetails(teacherInfo.getDetails());
        teacherDetails.setUserId(teacherInfo.getUserId());
        return teacherDetails;
    }
}
